package com.example.neusoft.yuejian_12.Model;

import com.example.neusoft.yuejian_12.Interface.UserFace;
import com.example.neusoft.yuejian_12.JavaBean.Photo;
import com.example.neusoft.yuejian_12.JavaBean.User;
import com.example.neusoft.yuejian_12.JavaBean.UserCollect;
import com.example.neusoft.yuejian_12.JavaBean.UserOrder;
import com.example.neusoft.yuejian_12.JavaBean.UserOrderInfo;
import com.example.neusoft.yuejian_12.Listener.PersonListener;
import com.example.neusoft.yuejian_12.Listener.UserAllOrderListener;
import com.example.neusoft.yuejian_12.Listener.UserCollectListener;
import com.example.neusoft.yuejian_12.Listener.UserHelpListener;
import com.example.neusoft.yuejian_12.Listener.UserListener;
import com.example.neusoft.yuejian_12.Listener.UserNoCollectListener;
import com.example.neusoft.yuejian_12.Listener.UserOrderInfoListener;
import com.example.neusoft.yuejian_12.Listener.UserPhotoListener;
import com.example.neusoft.yuejian_12.Service.UserService;

import java.util.List;

import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 晓真 on 2017/9/26.
 */

public class UserModel  implements UserFace {
    private UserService userService;
    private Retrofit retrofit;
    private static final String BASE_URL="http://172.24.10.175";

    public UserModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())//解析方式
                .build();
    }

    @Override
    public void Login(String tel, String password, final UserListener userListener) {
        userService=retrofit.create(UserService.class);
        Call<User> call=userService.Login(tel,password);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if (response.isSuccessful()&&response.body().getError()==null)
                    userListener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                userListener.onFail("登录失败!");
            }
        });
    }

    @Override
    public void Register(String user_name,String password, String tel, final UserListener userListener) {
        userService=retrofit.create(UserService.class);
        Call<User> call=userService.Register(user_name,password,tel);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                        String result = response.body().getError();
                        if(!"手机号已被使用".equals(result))
                            userListener.onResponse(response.body());
                        else{
                            userListener.onFail("手机号已存在");
                        }
                    }
            @Override
            public void onFailure(Call<User> call, Throwable t) {
                userListener.onFail("注册失败!");
            }
        });
    }

    @Override
    public void getUserByID(String SessionID, String userid,final PersonListener personListener) {
        userService = retrofit.create(UserService.class);
        Call<User> call =userService.getUserById("lgj98rfkiqvenhrk2o7oe2po81",userid);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call call, Response response) {
                if (response.isSuccessful() && response.body() != null) {
                    personListener.onResponse(response.body());
                } else {
                    personListener.onFail("123");
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                personListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void EditUserPass(String SessionID, String userid, String password, final PersonListener personListener) {
        userService = retrofit.create(UserService.class);
        Call<User> call =userService.EditUserPass("lgj98rfkiqvenhrk2o7oe2po81",userid,password);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call call, Response response) {
                if (response.isSuccessful() && response.body() != null) {
                    personListener.onResponse(response.body());
                } else {
                    personListener.onFail("123");
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                personListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void EditUserName(String SessionID, String userid, String user_name, final PersonListener personListener) {
        userService = retrofit.create(UserService.class);
        Call<User> call =userService.EditUserName("lgj98rfkiqvenhrk2o7oe2po81",userid,user_name);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call call, Response response) {
                if (response.isSuccessful() && response.body() != null) {
                    personListener.onResponse(response.body());
                } else {
                    personListener.onFail("123");
                }
            }
            @Override
            public void onFailure(Call call, Throwable t) {
                personListener.onFail(t.toString());
            }
        });
    }

    public Call<okhttp3.ResponseBody> EditUserPhoto(String userid, MultipartBody.Part file){
        UserService actImpls = retrofit.create(UserService.class);
        return  actImpls.EditUserPhoto(userid,file);
    }


    @Override
    public void getcollect(String userid, final UserCollectListener usercollectListener) {
        userService = retrofit.create(UserService.class);
        Call<List<UserCollect>> call =userService.getcollect("fans","lgj98rfkiqvenhrk2o7oe2po81",userid);
        call.enqueue(new Callback<List<UserCollect>>() {
            @Override
            public void onResponse(Call<List<UserCollect>> call, Response<List<UserCollect>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    usercollectListener.onResponse(response.body());
                } else {
                    usercollectListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<List<UserCollect>> call, Throwable t) {
                usercollectListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getShaituByUser(String userid, final UserPhotoListener userphotoListener) {
        userService = retrofit.create(UserService.class);
        Call<List<Photo>> call =userService.getShaituByUser("shaitu","lgj98rfkiqvenhrk2o7oe2po81",userid);
        call.enqueue(new Callback<List<Photo>>() {
            @Override
            public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    userphotoListener.onResponse(response.body());
                } else {
                    userphotoListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<List<Photo>> call, Throwable t) {
                userphotoListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getAllOrderByUser(String userid,final UserAllOrderListener listListener) {
        userService = retrofit.create(UserService.class);
        Call<List<UserOrder>> call =userService.getAllOrderByUser("sign",userid,"lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<List<UserOrder>>() {
            @Override
            public void onResponse(Call<List<UserOrder>> call, Response<List<UserOrder>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<UserOrder>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getOrderByType(String userid,String vstate,final UserAllOrderListener listListener) {
        userService = retrofit.create(UserService.class);
        Call<List<UserOrder>> call =userService.getOrderByType("sign",userid,vstate,"lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<List<UserOrder>>() {
            @Override
            public void onResponse(Call<List<UserOrder>> call, Response<List<UserOrder>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<UserOrder>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void getSignByID(String id, String type, final UserOrderInfoListener listListener) {
        userService = retrofit.create(UserService.class);
        Call<UserOrderInfo> call =userService.getSignById(id,type,"lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<UserOrderInfo>() {
            @Override
            public void onResponse(Call<UserOrderInfo> call, Response<UserOrderInfo> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<UserOrderInfo> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void NoSign(String id, String type, final UserOrderInfoListener listListener) {
        userService = retrofit.create(UserService.class);
        Call<UserOrderInfo> call =userService.NoSign(id,type,"lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<UserOrderInfo>() {
            @Override
            public void onResponse(Call<UserOrderInfo> call, Response<UserOrderInfo> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<UserOrderInfo> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void Nocollect(String user_id, String fan_id, String fans_type, final UserNoCollectListener listListener) {
        userService = retrofit.create(UserService.class);
        Call<Integer> call =userService.Nocollect(user_id ,fan_id,fans_type);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

    @Override
    public void UserHelp(String content, final UserHelpListener listListener) {
        userService = retrofit.create(UserService.class);
        Call<Integer> call =userService.UserHelp(content,"iamzjs@126.com","lgj98rfkiqvenhrk2o7oe2po81");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body() > 0) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }


}
